package com.zf.bm.common.core.sys.service;

import com.zf.bm.common.core.config.ConfigUtils;
import com.zf.bm.common.core.shiro.config.ShiroConfig;
import com.zf.bm.common.core.sys.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author Administrator
 */
@Slf4j
@Service
public class PasswdRetryLimitService {
    @Autowired
    private ShiroConfig config;

    @Autowired
    UserService userService;

    public void handle(String account, int num) {
        if (0 >= num) {
            //锁定用户
            Date date = new Date();
            //冻结30分钟
            Date frozenDate = new Date(date.getTime() + config.getFrozenDate() * (60 * 1000));
            userService.updateLoginFlag(account, ConfigUtils.NO, frozenDate);
            log.info("锁定用户:{}", account);
        }


    }


    public Date getFrozenDate(String account) {
        User user = new User();
        user.setLoginName(account);
        user.setDelFlag("0");
        user.setLoginFlag("0");
        user = userService.getByLoginName(user);
        if (null != user) {
            return user.getFrozenDate();
        } else {
            return null;
        }
    }
}
